package com.wjv.example.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.wjv.example.common.entity.ResultInfo;
import com.wjv.example.service.OrderFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Title: OrderController</p>
 * <p>Company:misspao </p>
 *
 * @author: wjv
 * @Date: Create in 16:35 2020/9/24
 * @Description:
 */
@RestController
@RequestMapping("/consumer/order")
@DefaultProperties(defaultFallback = "globalFallbackMethod")  //全局的
public class OrderController {

    @Autowired
    private OrderFeignService orderFeignService;

    @GetMapping("/get")
    public ResultInfo get(int id) {
        return orderFeignService.get(id);
    }

    @GetMapping("/get2")
    @HystrixCommand(fallbackMethod = "timeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")  //2秒钟以内就是正常的业务逻辑
    })
    public ResultInfo get2(int id) {
        return orderFeignService.get2(id);
    }

    @GetMapping("/get3")
    @HystrixCommand
    public ResultInfo get3() {
        int a = 10 / 0;
        return ResultInfo.ok("123");
    }

    /**
     * 指定的 后备方法
     * 此方法要和目标方法 参数、返回值 相同。
     *
     * @param id
     * @param throwable
     * @return
     */
    public ResultInfo timeoutHandler(int id, Throwable throwable) {
        throwable.printStackTrace();
        return ResultInfo.error("线程池：" + Thread.currentThread().getName() + " ; 80 系统繁忙, 请稍候再试");
    }

    /**
     * 全局fallback方法
     * @return
     */
    public ResultInfo globalFallbackMethod(){
        return ResultInfo.error("Global异常处理信息，请稍后再试,(┬＿┬)");
    }

}
